package com.wocao.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.*;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * OSS文件上传工具类
 */
public class OssUtils {

    //Oss服务器地址
    private static String endpoint = "oss-cn-wuhan-lr.aliyuncs.com";
    //密钥
    private static String accessKeyId = "LTAI5tHXPBeBhLnyKENecW2e";
    private static String accessKeySecret = "WL58qAe4Grli9TwVIdVLjvfhkqWR5c";
    //Oss服务器名
    private static String bucketName = "myfirstfukker";
    //bucket中的目录名
    public static String dir = "images";

    /**
     * 文件上传
     */
    public static void upload(InputStream inputStream, String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        oss.putObject(bucketName, dir + "/" + fileName, inputStream);
        oss.shutdown();
    }

    /**
     * 获得文件URL
     */
    public static String getURL(String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //设置过期时间
        LocalDateTime time = LocalDateTime.now().plusDays(100);
        Date expiration = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        URL url = oss.generatePresignedUrl(bucketName, dir + "/" + fileName, expiration);
        oss.shutdown();
        return url.toString();
    }


    /**
     * 多文件上传
     *
     * @param
     * @param fileNames
     */
    public static void uploadFiles(List<File> files, List<String> fileNames) {
        if (files.size() != fileNames.size()) {
            throw new IllegalArgumentException("文件和文件名要有相同的大小");
        }

        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);
                String fileName = fileNames.get(i);

                try (InputStream inputStream = new FileInputStream(file)) {
                    // 使用PutObjectRequest上传
                    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dir + "/" + fileName, inputStream);
                    PutObjectResult putObjectResult = oss.putObject(putObjectRequest);

                    // 获取上传文件的全路径
                    String fileUrl = getURLs(fileName);
                    System.out.println("文件上传成功, URL: " + fileUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println("上传文件失败，文件名: " + fileName);
                }
            }
        } finally {
            oss.shutdown();
        }
    }


    /**
     * 获取文件的URL
     *
     * @param fileName 文件名
     * @return 文件的URL
     */
    public static String getURLs(String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 设置过期时间为 100 天
            LocalDateTime time = LocalDateTime.now().plusDays(100);
            Date expiration = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());

            // 生成 URL
            URL url = oss.generatePresignedUrl(bucketName, dir + "/" + fileName, expiration);
            return url.toString();
        } finally {


        }
    }


}